package com.krain.corelibrary.widget;


import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.krain.corelibrary.R;


/**
 * Created by drakeet on 9/28/14.
 */
public class MaterialDialog extends Dialog {

    Context context;
    TextView mTitle;
    TextView mMsg;
    TextView mConfirm;
    TextView mCancel;
    boolean mCanDismiss = true;

    public MaterialDialog(Context context) {
        super(context);
        init(context);
    }

    public MaterialDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    protected MaterialDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    void init(Context context) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.lib_layout_materialdialog, null);
        setContentView(view);
        setCanceledOnTouchOutside(mCanDismiss ? true : false);
        mTitle = (TextView) view.findViewById(R.id.md_title);
        mMsg = (TextView) view.findViewById(R.id.md_message);
        mConfirm = (TextView) view.findViewById(R.id.md_confirm);
        mCancel = (TextView) view.findViewById(R.id.md_cancel);
    }

    public void setMessage(String string) {
        mMsg.setText(string);
    }

    public void setOnComfirmClick(final View.OnClickListener listener) {
        setOnComfirmClick(listener,false);
    }

    public void setOnComfirmClick(final View.OnClickListener listener,final boolean donotDismiss) {
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener !=null)
                    listener.onClick(v);
                if (!donotDismiss)
                    dismiss();
            }
        });
    }

    public void setOnCancelClick(final View.OnClickListener listener) {
        if (listener != null)
            mCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener !=null)
                        listener.onClick(v);
                    dismiss();
                }
            });
        else mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog.this.dismiss();
            }
        });
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(context.getString(titleId));
    }


    public void setConfirm(String confirm) {
        mConfirm.setText(confirm);
    }

    public void setCancel(String cancel) {
        mCancel.setText(cancel);
    }

    public void setCancelGone() {
        mCancel.setVisibility(View.GONE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && !mCanDismiss) return true;
        else
            return super.onKeyDown(keyCode, event);
    }

    public void setmCanDismiss(boolean mCanDismiss) {
        this.mCanDismiss = mCanDismiss;
        setCanceledOnTouchOutside(mCanDismiss);
    }

    public TextView getmMsg() {
        return mMsg;
    }
}


